package com.dynabook.ems.disruptor.BorrowUpdate;


import com.lmax.disruptor.ExceptionHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class BorrowCustomExceptionHandler implements ExceptionHandler<BorrowUpdateEvent> {

    @Override
    public void handleEventException(Throwable throwable, long l, BorrowUpdateEvent borrowUpdateEvent) {

        try {
            log.error("Event " + borrowUpdateEvent.getBorrowHistoryList().toString() + " failed with exception: " + throwable.getMessage());
            // 异常补偿操作
        } catch (Throwable t) {
            // 捕获处理过程中出现的异常，避免线程中断
            log.error("Event " + borrowUpdateEvent.getBorrowHistoryList().toString() + " failed with exception: " + throwable.getMessage());
        }

    }

    @Override
    public void handleOnStartException(Throwable ex) {
        // 处理消费者启动时的异常
        log.error("Consumer failed to start: " + ex.getMessage());
    }

    @Override
    public void handleOnShutdownException(Throwable ex) {
        // 处理消费者关闭时的异常
        // 可以在这里进行一些清理操作，如关闭资源等
        log.error("Consumer failed to shutdown: " + ex.getMessage());
    }
}
